package com.example.xui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xui.Way.BasisTimesUtils;
import com.example.xui.Way.QMUISet;
import com.example.xui.Way.Typefaces;
import com.example.xui.Way.changStatusIconCollor;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.style.cityjd.JDCityConfig;
import com.lljjcoder.style.cityjd.JDCityPicker;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;

import static com.example.xui.R.id.image1;
import static com.example.xui.R.id.text5;
import static com.example.xui.R.id.textView19;
import static com.example.xui.R.id.textView31;

public class Shezhi extends AppCompatActivity implements QMUISet {
    private TextView textView;
    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private TextView textView4;
    private TextView textView5;
    private TextView textView6;
    private ImageView imageView;
    private TextView name;
    private TextView collage;
    private TextView get_class;
    private TextView hobby;
    private String userID;
    private ImageView tijiao;
    private String uri;
    @BindViews({R.id.textView19, R.id.textView11, R.id.text5,R.id.textView25,R.id.textView30,R.id.textView22,R.id.textView33,R.id.textView37,R.id.textView43,R.id.textView40,R.id.textVie19,R.id.textVie37})
    public List<TextView> textViews;
    @BindView(R.id.imageView3) ImageView imageView1;
    private static PictureBean pictureBean;
    private String a=null;
    private String button=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shezhi);
        changStatusIconCollor changStatusIconCollor=new changStatusIconCollor();
        changStatusIconCollor.changStatusIconCollor1(getWindow(),true);
        ButterKnife.bind(this);
        init();
    }
    public void init(){
        textView=findViewById(R.id.textView21);
        textView1=findViewById(R.id.text6);
        textView2=findViewById(R.id.textView26);
        textView3=findViewById(R.id.textView31);
        textView4=findViewById(R.id.textView34);
        textView5=findViewById(R.id.textView38);
        textView6=findViewById(R.id.textView23);
        imageView=findViewById(R.id.imageView2);
        name=findViewById(R.id.textVie21);
        collage=findViewById(R.id.textVie38);
        get_class=findViewById(R.id.textView44);
        hobby=findViewById(R.id.textView41);
        tijiao=findViewById(R.id.tijaio);
        tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updata_people();
            }
        });
        userID=getIntent().getStringExtra("userID");
        for(int i=0;i<textViews.size();i++){
            textViews.get(i).setTypeface(Typefaces.get(this,"nai.ttf"));
        }
//        String userPicture=getIntent().getStringExtra("userPicture");
//        String userNickname=getIntent().getStringExtra("userNickname");
//        String userName=getIntent().getStringExtra("userName");
//        String userSex=getIntent().getStringExtra("userSex");
//        String userBirthday=getIntent().getStringExtra("userBirthday");
//        String userTel=getIntent().getStringExtra("userTel");
//        String userArea=getIntent().getStringExtra("userArea");
//        String userIntroduction=getIntent().getStringExtra("userIntroduction");
//        String userCollege=getIntent().getStringExtra("userCollege");
//        String userMajor=getIntent().getStringExtra("userMajor");
//        String userHobby=getIntent().getStringExtra("userHobby");
//        imageView.setImageBitmap(BitmapFactory.decodeFile("/storage/emulated/0/Android/data/com.example.xui/files/Pictures/1606517318058.jpg"));
        new Thread(){
            @Override
            public void run() {
                super.run();
                RequestParams params = new RequestParams("http://10.138.87.49:8080/user/userselect");
                params.addHeader("Content-Type","application/x-www-form-urlencoded");
                params.addBodyParameter("userID",userID);
                x.http().post(params, new Callback.CacheCallback<JSONObject>() {
                    @Override
                    public void onSuccess(JSONObject result) {
                        try {
                            String userPicture=result.getString("userPicture");
                            String userNickname=result.getString("userNickname");
                            String userName=result.getString("userName");
                            String userSex=result.getString("userSex");
                            String userBirthday=result.getString("userBirthday");
                            String userTel=result.getString("userTel");
                            String userArea=result.getString("userArea");
                            String userIntroduction=result.getString("userIntroduction");
                            String userCollege=result.getString("userCollege");
                            String userMajor=result.getString("userMajor");
                            String userHobby=result.getString("userHobby");
                            String userID=result.getString("userID");
                            uri=userPicture;
                            imageView.setImageBitmap(BitmapFactory.decodeFile(userPicture));
                            textView.setText(userNickname);
                            name.setText(userName);
                            textView1.setText(userID);
                            a=userNickname;
                            textView2.setText(userSex);
                            textView3.setText(userBirthday);
                            textView6.setText(userTel);
                            textView4.setText(userArea);
                            textView5.setText(userIntroduction);
                            collage.setText(userCollege);
                            get_class.setText(userMajor);
                            hobby.setText(userHobby);
                            imageView1.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    finish();
                                }
                            });

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Toast.makeText(Shezhi.this,"网络请求错误",Toast.LENGTH_SHORT).show();
                        Log.i("sd",params.toString());
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {
                        Toast.makeText(Shezhi.this,"请求已取消",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public boolean onCache(JSONObject result) {
                        return false;
                    }
                });
            }
        }.start();


      /*  String interest=null;
        interest=getIntent().getStringExtra("interest");
        Button textView=findViewById(R.id.textView41);
        textView.setText(interest);*/
    }
    public void onClickShowQMUIDialog(View view) {
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(Shezhi.this);
        builder.setTitle("昵称")
                .setPlaceholder("在此输入您的昵称")
                .setInputType(InputType.TYPE_CLASS_TEXT)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
                            textView.setText(text);
                            tijiao.setVisibility(View.VISIBLE);
                            Toast.makeText(getApplicationContext(), "您的昵称: " + text, Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                        } else {
                            Toast.makeText(getApplicationContext(), "请填入昵称", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .show();
    }

    public void onClickXueZe(View view){
        final String[] items = new String[]{"男", "女", "保密"};
        new QMUIDialog.MenuDialogBuilder(Shezhi.this)
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "你选择了 " + items[which], Toast.LENGTH_SHORT).show();
                        textView2.setText(items[which]);
                        tijiao.setVisibility(View.VISIBLE);
                        dialog.dismiss();
                    }
                })
                .show();
    }

    public void onClick(View v) {
        BasisTimesUtils.showDatePickerDialog(Shezhi.this, BasisTimesUtils.THEME_HOLO_LIGHT, "请选择年月日", 2015, 1, 1, new BasisTimesUtils.OnDatePickerListener() {

            @Override
            public void onConfirm(int year, int month, int dayOfMonth) {
                String text=year + "-" + month + "-" + dayOfMonth;
                Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
                String str3 = "<small>"+text+"</small>";
                textView3.setText(Html.fromHtml(str3));
                tijiao.setVisibility(View.VISIBLE);
            }

            @Override
            public void onCancel() {
                Toast.makeText(getApplicationContext(),"cancel",Toast.LENGTH_LONG).show();
            }
        });
    }

    public void Weizhi(View v){
        JDCityPicker cityPicker = new JDCityPicker();
        JDCityConfig jdCityConfig = new JDCityConfig.Builder().build();

        jdCityConfig.setShowType(JDCityConfig.ShowType.PRO_CITY_DIS);
        cityPicker.init(this);
        cityPicker.setConfig(jdCityConfig);
        cityPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                textView4.setText(province.getName()+" " + city.getName()+" "+district.getName());
                tijiao.setVisibility(View.VISIBLE);
            }

            @Override
            public void onCancel() {
                Toast.makeText(getApplicationContext(),textView3.getText(),Toast.LENGTH_SHORT).show();
            }
        });
        cityPicker.showCityPicker();
    }

    public void onClickShowQMUIDialog2(View view) {
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(Shezhi.this);
        builder.setTitle("简介")
                .setPlaceholder("在此输入您的简介")
                .setInputType(InputType.TYPE_CLASS_TEXT)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
                            textView5.setText(text);
                            Toast.makeText(getApplicationContext(), "您的简介: " + text, Toast.LENGTH_SHORT).show();
                            tijiao.setVisibility(View.VISIBLE);
                            dialog.dismiss();
                        } else {
                            Toast.makeText(getApplicationContext(), "请填入简介", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .show();
    }

    public void onClickShowQMUIDialog3(View view) {
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(Shezhi.this);
        builder.setTitle("手机号")
                .setPlaceholder("在此输入您的手机号")
                .setInputType(InputType.TYPE_CLASS_TEXT)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {
                            textView6.setText(text);
                            tijiao.setVisibility(View.VISIBLE);
                            Toast.makeText(getApplicationContext(), "您的手机号: " + text, Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                        } else {
                            Toast.makeText(getApplicationContext(), "请填入手机号", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .show();
    }

    public void interest(View v){
        Intent intent=new Intent(Shezhi.this,Interest.class);
        intent.putExtra("sno",userID);
        startActivity(intent);
        finish();
    }


    public void paizhao(View v){
        PictureSelector
                .create(Shezhi.this, PictureSelector.SELECT_REQUEST_CODE)
                .selectPicture(true);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /*结果回调*/
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE) {
            if (data != null) {
                pictureBean = data.getParcelableExtra(PictureSelector.PICTURE_RESULT);
                if (pictureBean.isCut()) {
                    imageView.setImageBitmap(BitmapFactory.decodeFile(pictureBean.getPath()));
                    Toast.makeText(Shezhi.this,"uri="+pictureBean.getPath(),Toast.LENGTH_SHORT).show();
                    Log.i("Tag","tag="+pictureBean.getPath());
                    uri=pictureBean.getPath();
                    tijiao.setVisibility(View.VISIBLE);
                } else {
                    imageView.setImageURI(pictureBean.getUri());
                    Toast.makeText(Shezhi.this,"uri="+pictureBean.getUri(),Toast.LENGTH_SHORT).show();
                }

                //使用 Glide 加载图片
                /*Glide.with(this)
                        .load(pictureBean.isCut() ? pictureBean.getPath() : pictureBean.getUri())
                        .apply(RequestOptions.centerCropTransform()).into(mIvImage);*/
            }
        }
    }

    public void updata_people(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                RequestParams params = new RequestParams("http://10.138.87.49:8080/user/userupdate");
                params.addHeader("Content-Type","application/x-www-form-urlencoded");
                params.addBodyParameter("userPicture",uri);
                params.addBodyParameter("userID",textView1.getText().toString());
                params.addBodyParameter("userNickname",textView.getText().toString());
                params.addBodyParameter("userSex",textView2.getText().toString());
                params.addBodyParameter("userBirthday",textView3.getText().toString());
                params.addBodyParameter("userArea",textView4.getText().toString());
                params.addBodyParameter("userIntroduction",textView5.getText().toString());
                x.http().post(params, new Callback.CacheCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        if(result.equals("success")){
                            Toast.makeText(Shezhi.this, "修改成功", Toast.LENGTH_SHORT).show();
                        }
                        else{
                            Toast.makeText(Shezhi.this, "修改失败", Toast.LENGTH_SHORT).show();
                            Log.i("rer",params.toString());
                        }

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Toast.makeText(Shezhi.this, "网络请求错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {
                        Toast.makeText(Shezhi.this, "请求已取消", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public boolean onCache(String result) {
                        return false;
                    }
                });
            }
        }.start();
    }
}
